/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示UART硬件流控功能
  *          本样例演示通过查询方式接收并发送16字节
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J12
  *                    短接J8、J11、J13
  *
  *          PC.TXD -> MCU.RXD @ PA04
  *          PC.RXD <- MCU.TXD @ PB02
  *          PC.CTS <- MCU.RTS @ PB01
  *          PC.RTS -> MCU.CTS @ PB00
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_uart.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/
#define BUF_SIZE 32

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
UART_HandleTypeDef huart2;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void Uart_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/
uint8_t TxRxBuf[BUF_SIZE];

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // UART及端口初始化
    Uart_Init();

    //------------------------------------------------------------
    // 每接收 BUF_SIZE 个字节，则返回 BUF_SIZE 个字节
    // 请通过串口助手向MCU.PA04发送数据,并从PB02接收数据
    // 切换PC端CTS信号电平，可观察数据的传输及停止
    while (1)
    {
        TxRxBuf[0] = huart2.Instance->RDR;                            // 清除RTS信号

        HAL_StatusTypeDef Flag = HAL_UART_Receive(&huart2, TxRxBuf, BUF_SIZE, 0);
        if (Flag == HAL_ERROR)
        {
            HAL_UART_CLR_FLAG(&huart2, UART_IT_FLAG_PE | UART_IT_FLAG_FE);
        }
        if (Flag == HAL_OK)
        {
            HAL_UART_Transmit(&huart2, TxRxBuf, BUF_SIZE, 0);
        }
    }
}


/*******************************************************************************
 * @brief  UART初始化
 * @param  None
 * @retval None
 */
static void Uart_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    // 时钟使能
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    BSP_UART_PERIPH_CLK_ENABLE();
    BSP_UART_TXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_RXD_PORT_PERIPH_CLK_ENABLE();

    // 配置UART RXD/TXD管脚
    BSP_UART_TXD_AF();
    GPIO_InitStruct.Pins = BSP_UART_TXD_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(BSP_UART_TXD_PORT, &GPIO_InitStruct);

    BSP_UART_RXD_AF();
    GPIO_InitStruct.Pins = BSP_UART_RXD_PIN;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(BSP_UART_RXD_PORT, &GPIO_InitStruct);

    // Config PB01 AS UART2.RTS
    AFx_UART2RTS_PB01();
    GPIO_InitStruct.Pins = GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    // Config PB00 AS UART2.CTS
    AFx_UART2CTS_PB00();
    GPIO_InitStruct.Pins = GPIO_PIN_0;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    // 配置 UART
    MemClr(&huart2, sizeof(huart2));

    huart2.Instance          = BSP_UART;
    huart2.RxDoneFun         = UART_RXDONE_FIXLEN;                    // 按照固定长度接收
    huart2.Init.ClockSource  = UART_CLKSRC_PCLK;
    huart2.Init.HwControl    = UART_HWCONTROL_CTS_RTS;                // CTS流控+RTS流控
    huart2.Init.PclkFreq     = 48000000;
    huart2.Init.BaudRate     = 115200;                                // Baud = 115200
    huart2.Init.ParityMode   = UART_PARITYMODE_NONE;                  // 无校验位
    huart2.Init.StopBits     = UART_STOPBITS_1;                       // STOP = 1

    HAL_UART_Init(&huart2);                                           // 初始化UART
    HAL_UART_TXD_Enable(huart2.Instance);                             // 使能UART的TXD功能
    HAL_UART_RXD_Enable(huart2.Instance);                             // 使能UART的RXD功能
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
